<template>
  <div style="display: flex; height: 100%">
    <department-screen v-model="list.filter.parentId" ref="department" />

    <div class="app-container" style="flex: 1">
      <el-form
        ref="listFilterRef"
        :model="list.filter"
        inline
        class="list-filter"
      >
        <el-form-item prop="departmentName">
          <el-input
            v-model="list.filter.departmentName"
            placeholder="部门名称"
            clearable
          />
        </el-form-item>
        <!-- <el-form-item prop="dataStatus">
            <KiSelect
              v-model="list.filter.dataStatus"
              :options="['未设置', '已设置']"
              placeholder="数据权限状态"
            />
          </el-form-item> -->
        <el-form-item>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- v-show="list.filter.parentId" -->
      <div class="table-operation">
        <AuthButton @click="c()" name="新增" />
      </div>

      <el-table v-loading="list.loading" :data="list.data" v-bind="tableProp">
        <el-table-column align="center" type="index" />
        <el-table-column label="部门名称" prop="departmentName" />
        <el-table-column label="电话" prop="phone" />
        <!-- <el-table-column label="数据权限状态">
            <template slot-scope="{row}">
              <el-tag v-if="row.dataStatus == 0" type="info">未设置</el-tag>
              <el-tag v-if="row.dataStatus == 1" type="success">已设置</el-tag>
            </template>
          </el-table-column> -->
        <el-table-column label="操作" align="center">
          <template slot-scope="{ row }">
            <AuthButton @click="$refs.deploy.show(row)" circle name="配置权限">
              <i class="el-icon-s-cooperation" />
            </AuthButton>
            <AuthButton @click="u({ id: row.departmentId })" name="编辑" />
            <AuthButton @click="d({ id: row.departmentId })" name="删除" />
          </template>
        </el-table-column>
      </el-table>
    </div>

    <KiFormDialog
      :show.sync="form.show"
      :title="formTitle"
      v-model="form.data"
      :submit="submit"
      :readonly="form.status === 'r'"
      ref="formRef"
      :loading="form.loading"
    >
      <template #el-form>
        <el-form-item
          label="名称"
          prop="departmentName"
          :verify="{ maxLength: 45 }"
        >
          <el-input
            v-model="form.data.departmentName"
            placeholder="请输入部门名称"
            clearable
          />
        </el-form-item>
        <el-form-item
          label="电话"
          prop="phone"
          :verify="{ required: false, maxLength: 20 }"
        >
          <el-input
            v-model="form.data.phone"
            placeholder="请输入部门电话"
            clearable
          />
        </el-form-item>
      </template>
    </KiFormDialog>

    <deploy ref="deploy" @closeDialog="getList()" />
  </div>
</template>

<script>
import pageMixin from '@/utils/pageMixin'
import useAdmateAdapter from '@/utils/useAdmateAdapter'
import deploy from './components/department/deploy.vue'
import departmentScreen from './components/department-screen'

let _this = null
export default {
  mixins: [pageMixin],
  components: { deploy, departmentScreen },
  setup: () =>
    useAdmateAdapter(
      {
        urlPrefix: 'sot-admin-api/sys/department',
        axiosConfig: {
          getList: {
            url: 'list'
          },
          d: {
            method: 'DELETE'
          }
        },
        list: {
          dataAt: 'data'
        }
      },
      {
        getListInitially: false,
        afterGetList (data, trigger) {
          if (['c', 'u', 'd'].includes(trigger)) {
            _this?.$refs.department?.getDepartments() // 列表改变时更新树
          }
        }
      }
    ),
  created () {
    _this = this
  },
  methods: {
    submit () {
      if (this.form.status === 'c') {
        this.form.data.parentId = this.list.filter.parentId
      }
      return this.submitForm()
    }
  }
}
</script>
